package com.brillilab.service.core.kit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.brillilab.dao.mapper.kit.KitRegularUsageMapper;
import com.brillilab.dao.mapper.kit.KitUsageMapper;
import com.brillilab.domain.enums.kit.KitEnum;
import com.brillilab.domain.po.kit.KitRegularUsage;
import com.brillilab.service.core.kit.IKitRegularUsageService;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.Assert;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 常用清单
 *
 * @author wmh
 */
@Service
@Transactional
public class KitRegularUsageServiceImpl implements IKitRegularUsageService {

    @Autowired
    private KitRegularUsageMapper kitRegularUsageMapper;

    @Override
    public boolean insert(KitRegularUsage kitRegularUsage) {
        return kitRegularUsageMapper.insert(kitRegularUsage) == 1;
    }

    @Override
    public List<KitRegularUsage> selectList(Long groupId) {
        return kitRegularUsageMapper.selectList(new LambdaQueryWrapper<KitRegularUsage>().eq(KitRegularUsage::getGroupId,groupId));
    }

    @Override
    public int deleteBatch(List<Long> ids) {
        return kitRegularUsageMapper.deleteBatchIds(ids);
    }

    @Override
    public List<KitRegularUsage> selectList(Long labId,Long labMemberId,Date usageTime) {
        return kitRegularUsageMapper.selectList(new LambdaQueryWrapper<KitRegularUsage>()
                .eq(KitRegularUsage::getLabId,labId)
                .eq(KitRegularUsage::getLabMemberId,labMemberId)
                .eq(KitRegularUsage::getUsageTime,usageTime))
                .stream().peek(e ->{
                    if(KitEnum.Classify.SAMPLE.getValue().equals(e.getClassify())){
                        e.setKitType(2);
                    }
                }).collect(Collectors.toList());
    }

    @Override
    public List<KitRegularUsage> selectList(List<Long> groupIds) {
        Assert.isTrue(groupIds!=null,"参数缺失！");
        return kitRegularUsageMapper.selectListByGroupIds(groupIds).stream().peek(e ->{
            if(KitEnum.Classify.SAMPLE.getValue().equals(e.getClassify())){
                e.setKitType(2);
            }
        }).collect(Collectors.toList());
    }
}
